﻿
using System.Windows;
using System;
using System.IO;
using MyFloridaHouse.libSvUtilities.Logging;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;
using MyFloridaHouse.libSvCommonUtilities;
using MyFloridaHouse.libSvOpenDistricts.Dialogs;


namespace MyFloridaHouse.libSvOpenDistricts.ViewModel
{
    public partial class ViewModelBase
    {
        FileListDlg fileListDlg = null;

        private bool CmdFileImportCloudCanExecute(object parameter)
        {
            return true;
        }

        private void CmdFileImportCloudExecute(object parameter)
        {
            if (districtPlan.districtCount > 0)
            {
                PlanOverwriteConfirmDlg planOverwriteConfirmDlg = new PlanOverwriteConfirmDlg(OnMemberPropertyChanged, DisplayPanelForDialogs);
                planOverwriteConfirmDlg.Start(StartFileImportCloud);
            }
            else
            {
                StartFileImportCloud(true);
            }
        }


        private void StartFileImportCloud(bool result)
        {
            if (result)
            {
                serviceDAL.GetFileInfo(new System.EventHandler<GetFileInfoCompletedEventArgs>(GetFileInfoCompleted));
            }
        }


        private void GetFileInfoCompleted(object sender, GetFileInfoCompletedEventArgs args)
        {
            // error in WCF service call
            if (Log.HandleException(args.Error)) return;

            // unknown error in the service method
            if (args.Result == null)
            {
                MessageBox.Show("Error getting File Info from service " + args.Error.Message.ToString());
            }
            else
            {
                // caught error in the service method
                if (Log.HandleServerError("GetFileInfoCompleted", args.Result.errorMessage)) return;

                // Everything OK!

                //string filter = "";
                //districtPlan.SetupFileImport(out filter);
                fileListDlg = new FileListDlg(OnMemberPropertyChanged, DisplayPanelForDialogs);
                fileListDlg.fileNameList = args.Result.files;
                fileListDlg.Start(OnCloudFileSelected); 
            }
        }

        private void OnCloudFileSelected(bool result)
        {
            if (result)
            {
                try
                {
                    if (fileListDlg.selectedFileInfo != null)
                    {
                        serviceDAL.GetFile(fileListDlg.selectedFileInfo, new System.EventHandler<GetFileCompletedEventArgs>(GetCloudFileCompleted));
                    }
                }
                catch (Exception ex)
                {
                    
                }
            }
        }


        public void GetCloudFileCompleted(object sender, GetFileCompletedEventArgs args)
        {
            fileListDlg = null;

            // error in WCF service call
            if (Log.HandleException(args.Error)) return;

            // unknown error in the service method
            if (args.Result == null)
            {
                Log.HandleServerError("GetCloudFileCompleted", "Unknown Error getting File from Service ");
            }
            else
            {
                // caught error in the service method
                if (Log.HandleServerError("GetCloudFileCompleted", args.Result.errorMessage)) return;

                // Everything OK!
                if (args == null)
                {
                    Log.HandleInternalError("GetCloudFileCompleted: Unable to load file, invalid file name selected");
                    return;
                }

                try
                {
                    MemoryStream fileDataStream;
                    //TODO: Instead of using format = KMZ from fileinfo to indicate file shouldn't be decompressed, should use new boolean table field called "isCompressed"
                    if (args.Result.fileInfo.format.ToUpper() == "KMZ")
                    {
                        // kmz is already compressed
                        fileDataStream = new MemoryStream(args.Result.fileData);
                    }
                    else
                    {
                        fileDataStream = new MemoryStream(CompressionManager.DecompressToByteArray(args.Result.fileData));
                    }
                    string[] fileNamePiecesArray = args.Result.fileInfo.fileName.Split('.');
                    if (fileNamePiecesArray.Length > 1)
                    {
                        DistrictPlanLoadFile(fileDataStream, fileNamePiecesArray[1]);
                    }

                }
                catch (Exception ex)
                {
                    Log.HandleInternalError("GetCloudFileCompleted: Error opening cloud file, " + ex.Message.ToString());
                }
                finally
                {
                    // TODO: Indicate file load is going on
                    //isFileLoading = false;
                }

            }
        }

    }
}
